package com.controller;

import com.bean.Enterprise;
import com.service.FlowerService;
import com.service.impl.FlowerServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class FlowerPhotoServlet extends HttpServlet {

    private FlowerService flowerService = new FlowerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println("过来了！");
        Enterprise enterprise = (Enterprise) req.getSession().getAttribute("enterprise");
        String enterpriseid = enterprise.getEnterpriseid();
        //判断
        if (ServletFileUpload.isMultipartContent(req)) {
            //
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            //
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            try {
                List<FileItem> list = servletFileUpload.parseRequest(req);
                String flower_photo = "";
                Integer flowerid = null;
                for (FileItem fileItem : list) {
                    if (fileItem.isFormField()) {
                        System.out.println("文件名" + fileItem.getFieldName());
                        System.out.println("属性值" + fileItem.getString());
                        flowerid = Integer.parseInt(fileItem.getString());
                    } else {
                        System.out.println(fileItem.getFieldName());
                        flower_photo = enterpriseid + flowerid + ".jpg";
                        System.out.println(flower_photo);
                        String imgpath = "D:\\IDEAProgram\\onlineflower\\web\\static\\picFlower\\" + flower_photo;
                        File file = new File(imgpath);
                        if (file.exists()) {
                            file.delete();
                        }
                        fileItem.write(new File("D:\\IDEAProgram\\onlineflower\\web\\static\\picFlower\\" + flower_photo));
                    }
                }
                try {
                    flowerService.updateflowerPhoto(flowerid, enterpriseid, flower_photo);
                    req.setAttribute("mess", "更换成功！");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                req.getRequestDispatcher("/page/enterprise/modifyFlowerPhoto.jsp").forward(req, resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
